<?php
namespace app\banquet\model;

use app\BaseModel;
use think\facade\Db;

class Place extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_banquet_place';

    protected $append = [

        'package',

        'content'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-12-23 15:47
     * @功能说明:报名信息
     */
    public function getContentAttr($value,$data){

        if(!empty($data['id'])){

            $content_model = new PlaceContent();

            $list = $content_model->where(['place_id'=>$data['id']])->column('content_id');

            return array_values($list);

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-23 15:47
     * @功能说明:套餐
     */
    public function getPackageAttr($value,$data){

        if(!empty($data['id'])){

            $package_model = new PlacePackage();

            $list = $package_model->where(['place_id'=>$data['id']])->column('package_id');

            return array_values($list);

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-23 15:49
     * @功能说明:
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(isset($data['date'])){

            $date = $data['date'];

            unset($data['date']);

        }

        if(isset($data['content'])){

            $content = $data['content'];

            unset($data['content']);
        }

        if(isset($data['package'])){

            $package = $data['package'];

            unset($data['package']);
        }

        if(!empty($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        if(isset($date)&&isset($package)&&isset($content)){

            $this->updateSome($date,$package,$content,$id,$data['uniacid']);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['date'])){

            $date = $data['date'];

            unset($data['date']);

        }

        if(isset($data['package'])){

            $package = $data['package'];

            unset($data['package']);
        }

        if(isset($data['content'])){

            $content = $data['content'];

            unset($data['content']);
        }

        if(!empty($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        $res = $this->where($dis)->update($data);

        if(isset($date)&&isset($package)&&isset($content)){

            $this->updateSome($date,$package,$content,$dis['id'],$data['uniacid']);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-23 15:25
     * @功能说明:
     */
    public function updateSome($date,$package,$content,$id,$uniacid){

        $date_model    = new PlaceDate();

        $package_model = new PlacePackage();

        $place_content = new PlaceContent();

        $package_model->where(['place_id'=>$id])->delete();

        if(!empty($package)){

            foreach ($package as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'place_id'=> $id,

                    'package_id' => $value
                ];

                $package_model->dataAdd($insert);

            }

        }

        $place_content->where(['place_id'=>$id])->delete();

        if(!empty($content)){

            foreach ($content as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'place_id'=> $id,

                    'content_id' => $value
                ];

                $place_content->dataAdd($insert);

            }

        }

        if(!empty($date)){

            foreach ($date as $vs){

                $dis = [

                    'place_id' => $id,

                    'time'     => $vs['time'],
                ];

                $find = $date_model->dataInfo($dis);

                $insert = [

                    'place_id' => $id,

                    'time'     => $vs['time'],

                    'uniacid'  => $uniacid,

                    'date'     => date('Y-m-d',$vs['time']),

                    'month_date' => date('Y-m',$vs['time']),

                    'noon_price' => $vs['noon_price'],

                    'night_price' => $vs['night_price'],

                    'status' => $vs['status'],

                    'title' => $vs['title'],

                ];

                if(!empty($find)){

                    $date_model->dataUpdate($dis,$insert);

                }else{

                    $date_model->dataAdd($insert);

                }

            }

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }








}